import {fileURLToPath, URL} from 'node:url'
import {resolve} from 'node:path'
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import dts from 'vite-plugin-dts'

export default defineConfig({
    plugins: [
        vue(),
        vueDevTools(),
        dts({
            include: ['morghulis/**/*.ts', 'morghulis/**/*.vue', 'morghulis/**/*.d.ts', 'index.d.ts'],
            outDir: 'dist',
            staticImport: true,
            insertTypesEntry: true,
            aliasesExclude: ['vue'],
            compilerOptions: {
                skipLibCheck: true, 
                noImplicitAny: false
            }
        }),
    ],
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        },
    },
    build: {
        outDir: 'dist',
        lib: {
            entry: resolve(__dirname, 'morghulis/index.ts'),
            name: 'morghulis',
            fileName: 'index'
        },
        rollupOptions: {
            external: ['vue', 'element-plus', '@fortawesome/fontawesome-svg-core', '@fortawesome/free-solid-svg-icons', '@fortawesome/vue-fontawesome', '@vueuse/core'],
            output: {
                globals: {
                    vue: 'Vue',
                    'element-plus': 'ElementPlus',
                    '@fortawesome/fontawesome-svg-core': 'FontawesomeSvgCore',
                    '@fortawesome/free-solid-svg-icons': 'FreeSolidSvgIcons',
                    '@fortawesome/vue-fontawesome': 'VueFontawesome',
                    '@vueuse/core': 'VueUseCore'
                },
                assetFileNames: (assetInfo) => {
                    if (assetInfo.name && assetInfo.name.endsWith('.css')) {
                        return 'index.css';
                    }
                    return assetInfo.name || 'unknown';
                }
            }
        }
    },
    server: {
        open: false,
        proxy: {
            '/api': {
                target: 'http://127.0.0.1:8000',
                changeOrigin: true,
                ws: true,
                rewrite: path => path.replace(/^\/api/, '')
            },
            '/dfs': {
                target: 'http://10.134.10.92:9001',
                changeOrigin: true,
                rewrite: path => path.replace(/^\/dfs/, ''),
            },
        },
    },
})
